package reesoft.certificate.agent;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import reesoft.certificate.CertificateConfig;
import reesoft.certificate.CertificateErrorCode;
import reesoft.certificate.CertificateManager;
import reesoft.certificate.SecretKeyManager;
import reesoft.certificate.entity.SignRequest;
import reesoft.common.code.ErrorCode;
import reesoft.common.entity.Result;
import reesoft.common.util.StringUtil;

/**
 * 数字证书代理实现类
 *
 * Created by reesoft on 2017-07-18
 */
@Component
public class CertificateAgentImpl implements CertificateAgent
{
    @Autowired
    private CertificateManager certificateManager;

    @Autowired
    private SecretKeyManager secretKeyManager;

    @Autowired
    private CertificateConfig certificateConfig;

    @Override
    public Result<String> signData(String id, String data, String algorithm)
    {
        if (id == null || data == null || algorithm == null)
        {
            return new Result<>(ErrorCode.INVALID_PARAMETER);
        }

        if ("RSA".equalsIgnoreCase(algorithm))
        {
            return certificateManager.signData(id, data);
        }
        else if ("MD5".equalsIgnoreCase(algorithm))
        {
            return secretKeyManager.signData(id, data);
        }
        else
        {
            return new Result<>(CertificateErrorCode.ALGORITHM_NOT_SUPPORTED);
        }
    }

    @Override
    public Result verifySignData(String id, String data, String algorithm, String signature)
    {
        if (id == null || data == null || algorithm == null || signature == null)
        {
            return new Result<>(ErrorCode.INVALID_PARAMETER);
        }

        if ("RSA".equalsIgnoreCase(algorithm))
        {
            return certificateManager.verifySignData(id, data, signature);
        }
        else if ("MD5".equalsIgnoreCase(algorithm))
        {
            return secretKeyManager.verifySignData(id, data, signature);
        }
        else
        {
            return new Result(CertificateErrorCode.ALGORITHM_NOT_SUPPORTED);
        }
    }

    @Override
    public Result signRequest(SignRequest request)
    {
        if (request == null)
        {
            return new Result(ErrorCode.INVALID_PARAMETER);
        }

        if (StringUtil.isEmpty(request.getAppId()))
        {
            request.setAppId(certificateConfig.getSignAppId());
        }

        if (StringUtil.isEmpty(request.getSignType()))
        {
            request.setSignType(certificateConfig.getSignType());
        }

        Result<String> result = signData(request.getAppId(), request.generateSignData(), request.getSignType());

        if (result.isSuccess())
        {
            request.setSignature(result.getData());
        }

        return result;
    }

    @Override
    public Result verifySignRequest(SignRequest request)
    {
        if (request == null
                || StringUtil.isBlank(request.getSignType()) || StringUtil.isBlank(request.getSignature()))
        {
            return new Result(ErrorCode.INVALID_PARAMETER);
        }

        return verifySignData(request.getAppId(), request.generateSignData(), request.getSignType(), request.getSignature());
    }
}
